﻿using System.Drawing;
//using Styx.Logic;
//using Styx.Logic.Combat;
//using Styx.Logic.POI;
//using Styx.Logic.Pathing;
using System;

//using Styx.Plugins.PluginClass;
using Styx;
using Styx.Common;
using Styx.Common.Helpers;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.CommonBot.Inventory;
using Styx.CommonBot.POI;
using Styx.Helpers;
using Styx.Plugins;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.DBC;
using Styx.WoWInternals.WoWObjects;

namespace AssistFollow
{
    public class AssistFollow : HBPlugin
    {
        //Normal Stuff.
        public override string Name { get { return "AssistFollow"; } }
        public override string Author { get { return "Inrego [Millz Edit]"; } }
        public override Version Version { get { return new Version(1, 0); } }
        public override bool WantButton { get { return false; } }
        public override string ButtonText { get { return "None"; } }
        //Logging Class for your conviance
        public static void slog(string format, params object[] args)
        {
            Logging.Write(LogLevel.Normal, "[AssistFollow]: " + format, args);
        }
		
        private WoWPlayer AssistTarget
        {
            get 
            { 
                var party = StyxWoW.Me.PartyMembers;
                var assistTarget = StyxWoW.Me.PartyMembers[0];
				//slog("If not specified, we will assist " + assistTarget);
                foreach (WoWPlayer player in party)
                {
                    if (player.Name.Equals("ENTER LEAD CHARACTER NAME HERE"))
                        assistTarget = player;
                }
				//slog("Assisting [" + assistTarget + "]");
                return assistTarget;
            }
        }
        private static readonly LocalPlayer Me = Styx.StyxWoW.Me;
        
        Mount.MountWrapper mount = Mount.GroundMounts[0];
        //Uncomment if adding a UI for the plugin
        /*public override void OnButtonPress()
        {

        }*/

        public static WoWPoint Location()
        {
            return WoWPoint.Empty;
        }

		public static bool IsGameStable()
        {
            return StyxWoW.IsInGame && Me != null && Me.IsValid;
        }
		
        public override void Pulse()
        {
			//slog ("--------------------");
			//slog ("Leader Mounted? - " + AssistTarget.Mounted);
			//slog ("Me Mounted? - " + Me.Mounted);
			//slog ("Can Mount? - " + Mount.CanMount());
            if (RoutineManager.Current.NeedRest || RoutineManager.Current.NeedPreCombatBuffs || RoutineManager.Current.NeedPullBuffs || RoutineManager.Current.NeedHeal)
                return;
            if (AssistTarget == null)
                return;
            if (Me.Combat || Me.IsCasting)
                return;
            if (AssistTarget.Mounted && !Me.Mounted && Mount.CanMount())
			{
				slog("Mounting Up...");
                Mount.MountUp( Location );
				StyxWoW.SleepForLagDuration();
				
				while (IsGameStable() && Me.CurrentHealth > 1 && Me.IsCasting)
				{
					System.Threading.Thread.Sleep(75);
				}
			}
			if (!AssistTarget.Mounted && Me.Mounted && Me.Location.Distance(AssistTarget.Location) < 20)
			{
				slog("Dismounting...");
				Mount.Dismount();
			}
            if (AssistTarget.GotTarget && AssistTarget.CurrentTarget.IsTargetingMyPartyMember)
            {
                if (AssistTarget.CurrentTarget != Me.CurrentTarget)
                    AssistTarget.CurrentTarget.Target();
                //if (Me.Mounted)
				//	slog("Dismounting...");
                //    Mount.Dismount();
                if (Me.CurrentTarget.Location.Distance(Me.Location) > 10 && !Me.IsMoving)
                    WoWMovement.ClickToMove(WoWMovement.CalculatePointFrom(Me.CurrentTarget.Location, 5));
                if (Me.CurrentTarget.IsTargetingMyPartyMember)
                    BotPoi.Current = new BotPoi(Me.CurrentTarget, PoiType.Kill);
            }
            else if (Me.Location.Distance(AssistTarget.Location) > 15)
            {
				slog("Moving closer to " + AssistTarget.Name);
                MoveToTarget(AssistTarget, 10);
            }
        }
        private void MoveToTarget(WoWUnit target, int distance)
        {
            var location = WoWMovement.CalculatePointFrom(target.Location, distance);
            if (Me.Z - location.Z >= 5)
            {
                WoWMovement.ClickToMove(location);
            }
            else
            {
                Navigator.MoveTo(location);
            }
        }
        private void MountUp()
        {
            SpellManager.Cast(mount.CreatureSpell);
        }
        private bool CanMount()
        {
            return mount.CanMount;
        }
    }
}

